//
//  SDWeatherView.m
//  SuperDoctor
//
//  Created by 李忠 on 7/5/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDWeatherView.h"
#import "SDWeatherModel.h"
#import "UIImageView+WebCache.h"
#import "SDUnderlineButton.h"

#define padding 10

@interface SDWeatherView ()

//天气图片
@property (nonatomic, strong) UIImageView *nightPicture;
//天气
@property (nonatomic, strong) UILabel *weather;
//当前城市
@property (nonatomic, strong) SDUnderlineButton *currentCity;
//分割线
@property (nonatomic, strong) UIView *separateVW;
//温度
@property (nonatomic, strong) UILabel *temperature;
//日期
@property (nonatomic, strong) UILabel *dateLB;
//当天的天气
@property (nonatomic, strong) SDWeatherModel *curWeather;
//以后几天的天气
@property (nonatomic, strong) NSArray<SDWeatherModel *> *oherWeather;

@end

@implementation SDWeatherView



- (void)layoutSubviews{
    [super layoutSubviews];
    [self setupLayout];
}
- (void)setupLayout{
    
    [self.nightPicture mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.mas_centerY);
        make.leading.mas_equalTo(padding);
        make.height.width.mas_equalTo(padding*4);
        
    }];
    [self.weather mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.mas_equalTo(self.mas_centerY);
        make.leading.mas_equalTo(self.nightPicture.mas_trailing).mas_offset(padding);
        make.trailing.mas_equalTo(self.currentCity.mas_leading).mas_offset(-padding);
        
    }];
    [self.currentCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.mas_centerY);
        make.trailing.mas_equalTo(self.separateVW.mas_leading).mas_offset(-2*padding);
    }];
    [self.separateVW mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(padding);
        make.bottom.mas_equalTo(-padding);
        make.width.mas_equalTo(1);
    }];
    
    [self.temperature mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.mas_centerY).mas_offset(-padding * 0.3);
        make.leading.mas_equalTo(self.dateLB.mas_leading).mas_offset(padding);
    }];
    [self.dateLB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_centerY).mas_offset(padding * 0.3);
        make.leading.mas_equalTo(self.separateVW.mas_trailing).mas_offset(2*padding);
    }];
}


#pragma mark
#pragma mark - 城市按钮单击方法
- (void)cityClickButton:(UIButton *)sender{
    if (self.selectCityBlock) {
        _selectCityBlock();
    }
}

#pragma mark
#pragma mark - 重写set 方法
- (void)setDataList:(NSArray *)dataList{
    _dataList = dataList;
    self.curWeather = dataList[0];
    
    NSMutableArray *temp = dataList.mutableCopy;
    [temp removeObjectAtIndex:0];
    self.oherWeather = [NSArray arrayWithArray:temp.copy];
}
- (void)setCurWeather:(SDWeatherModel *)curWeather{
    _curWeather = curWeather;
    
    [self.nightPicture sd_setImageWithURL:[NSURL URLWithString:curWeather.nightPictureUrl]];
    self.weather.text = curWeather.weather;
    
    [self.currentCity setTitle:curWeather.currentCity forState:UIControlStateNormal];
    
    self.temperature.text = [NSString stringWithFormat:@"温度: %@",curWeather.temperature];
    NSString *dateString = curWeather.date;
    dateString = [[dateString componentsSeparatedByString:@"("] firstObject];
    self.dateLB.text = [NSString stringWithFormat:@"%@",dateString];
    
    
}
- (void)setOherWeather:(NSArray<SDWeatherModel *> *)oherWeather{
    _oherWeather = oherWeather;
    
}

#pragma mark
#pragma mark - 懒加载
- (UIImageView *)nightPicture{
    if (_nightPicture == nil) {
        
        _nightPicture = [[UIImageView alloc] init];
        [self addSubview:_nightPicture];
        
    }
    return _nightPicture;
}
- (UILabel *)weather{
    if (_weather == nil) {
        _weather = [[UILabel alloc] init];
        _weather.textColor = kThemeColor;
        _weather.numberOfLines = 0;
        _weather.font = FONT(12);
        [self addSubview:_weather];
    }
    return _weather;
}
- (UIButton *)currentCity{
    if (_currentCity == nil) {
        _currentCity = [SDUnderlineButton buttonWithType:UIButtonTypeCustom];
        _currentCity.titleLabel.font = FONT(12);
        _currentCity.titleLabel.numberOfLines = 0;
        [_currentCity setTitleColor:kThemeColor forState:UIControlStateNormal];
        [_currentCity addTarget:self action:@selector(cityClickButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_currentCity];
    }
    return _currentCity;
}
- (UIView *)separateVW{
    if (_separateVW == nil) {
        _separateVW = [[UIView alloc] init];
        _separateVW.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_separateVW];
    }
    return _separateVW;
}
- (UILabel *)temperature{
    if (_temperature == nil) {
        _temperature = [[UILabel alloc] init];
        _temperature.textColor = kThemeColor;
        _temperature.font = FONT(12);
        [self addSubview:_temperature];
    }
    return _temperature;
}
- (UILabel *)dateLB{
    if (_dateLB == nil) {
        _dateLB = [[UILabel alloc] init];
        _dateLB.textColor = kThemeColor;
        _dateLB.font = FONT(12);
        [self addSubview:_dateLB];
    }
    return _dateLB;
}



@end
